﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Vanfa.Entities.User;
using Vanfa.Biz.User;
using Vanfa.Common;
using Vanfa.Web.UI;

namespace Vanfa.Web
{
    public partial class Register : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Display(1);
            }
        }

        protected void lbNext_Click(object sender, EventArgs e)
        {
            if (!IsValid)
                return;

            if (!this.CheckUserInfo(txtUserName.Text.Trim(), 1))
            {
                Toolkit.MessageBox("用户名已经存在！", this);
                return;
            }
            if (!this.CheckUserInfo(txtMobile.Text.Trim(), 2))
            {
                Toolkit.MessageBox("电话号码已经存在！", this);
                return;
            }
            litMobile.Text = txtMobile.Text;
            Display(2);
        }

        protected void lbGotoStep1_Click(object sender, EventArgs e)
        {
            Display(1);
        }

        protected void lbRegister_Click(object sender, EventArgs e)
        {
            if (!Page.IsValid)
                return;

            var rg = this.SetRegistDetail();
            if (!this.CheckUserInfo(txtUserName.Text.Trim(), 1))
            {
                Toolkit.MessageBox("用户名已经存在！", this);
                return;
            }
            if (!this.CheckUserInfo(txtMobile.Text.Trim(), 2))
            {
                Toolkit.MessageBox("电话号码已经存在！", this);
                return;
            }
            if (!UserMag.CheckUserCode(txtMobile.Text.Trim(), txtCode.Text.Trim()))
            {
                this.codeError.Visible = true;
                return;
            }
            UserMag.Regist(rg);
            base.CreateTicket(new vUserTicket
            {
                UserID = rg.UserID,
                UserName = txtUserName.Text,
                UserType = 1,
                Mobile = txtMobile.Text.Trim()
            });
            Display(3);
        }

        private void Display(int step)
        {
            phRegStep1.Visible = step == 1;
            phRegStep2.Visible = step == 2;
            phRegStep3.Visible = step == 3;
        }

        /// <summary>
        /// 设置注册信息
        /// </summary>
        /// <returns></returns>
        private vRegist SetRegistDetail()
        {
            var reg = new vRegist();
            reg.UserName = txtUserName.Text.Trim();
            reg.Pwd = txtPwd.Text.Trim();
            reg.Type = rbPrivate.Checked ? EU_UserType.Private : EU_UserType.Group;
            reg.Grade = EU_UserGrade.Common;
            reg.MobileNumber = txtMobile.Text.Trim();
            return reg;
        }

        /// <summary>
        /// 检测用户信息
        /// </summary>
        /// <param name="info"></param>
        /// <param name="t"></param>
        /// <returns></returns>
        private bool CheckUserInfo(string info, int t)
        {
            if (t == 1 && UserMag.CheckUserName(info) == RetCheckUnique.User_Exists)
                return false;
            else if (t == 2 && UserMag.CheckMobile(info) == RetCheckUnique.Mobile_Exists)
                return false;
            return true;
        }
    }
}